package com.zuihai.app.mvp.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.jess.arms.utils.KnifeUtil;
import com.zuihai.app.R;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2017/6/30 0030.
 */

public class DealDialog extends Dialog {
    private Build mBuild;

    @BindView(R.id.iv_logo)
    CircleImageView mLogoView;

    @BindView(R.id.tv_content)
    TextView mContent;


    public DealDialog(Build build){
        this(build.mContext, R.style.centerDialog);
        mBuild=build;
    }

    public DealDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_deal);
        KnifeUtil.bindTarget(this,this);
        initWindow();
        init();
    }

    private void init() {
        if (mBuild.mLogo != 0){
            mLogoView.setVisibility(View.VISIBLE);
            mLogoView.setImageResource(mBuild.mLogo);
        }
        if (mBuild.mBuildContent!=null){
            mContent.setText(mBuild.mBuildContent);
        }
    }

    private void initWindow() {
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        setCanceledOnTouchOutside(true);
    }


    /**
     * 取消
     * @param view
     */
    @OnClick(R.id.tv_cancle)
    public void onCancle(View view){
        dismiss();
       /* if (mBuild.mCancleListener != null){
            mBuild.mCancleListener.onClick(view);
        }*/
    }
    @OnClick(R.id.tv_confirm)
    public void onConfirm(View view){

        if (mBuild.mConfirmListener != null){
            mBuild.mConfirmListener.onClick(this, view);
        }
    }

    public static class Build {

        private View.OnClickListener mCancleListener;
        private OnConfirmListener mConfirmListener;
        private Context mContext;
        private int mLogo;
        private String mBuildContent;

        public Build(Context context) {
            mContext = context;
        }

        public DealDialog create() {
            DealDialog dialog = new DealDialog(this);
            return dialog;
        }

        public Build setConfirmListener(OnConfirmListener mConfirmListener) {
            this.mConfirmListener = mConfirmListener;
            return this;

        }
        public Build setCancelListener(View.OnClickListener mCancel){
            this.mCancleListener=mCancel;
            return this;
        }
        public Build setLogoResuorce(@DrawableRes int drawable){
            this.mLogo = drawable;
            return this;
        }
        public Build setTextResource(String content){
            this.mBuildContent=content;
            return this;
        }
    }
    public interface OnConfirmListener{
        void onClick(DealDialog dialog, View v);
    }
}
